<?php
namespace app\admin\controller;
use app\common\controller\Admin ; 

class Useseal extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model =model('useseal');
    }

//----------------------------------------------------------------------
    /**
     * 我的申请 list
     * @return [type] [description]
     */
    public function myapplylist(){


        $user_id = session('user_auth.user_id');

        $map= array();
        $map['type_id']=1;//  维护请求
        $map['apply_user_id'] = $user_id;
        $map['status'] = ['>',-2];

        $list = model('useseal')
                    ->where($map)
                    ->order( 'id desc' )
                    ->paginate(20,false,['query' => request()->param()]);

        $data= array(
            'list' => $list,
        );

        $this->assign($data);
        $this->setMeta('系统管理|用章申请');
        return view();
    }



//----------------------------------------------------------------------
    /**
     * 维护申请 list
     * @return [type] [description]
     */
    public function whmyapplylist(){


        $user_id = session('user_auth.user_id');

        $map= array();
        $map['type_id']=2;//  维护请求
        $map['apply_user_id'] = $user_id;
        $map['status'] = ['>',-2];

        $list = model('useseal')
                    ->where($map)
                    ->order( 'id desc' )
                    ->paginate(20,false,['query' => request()->param()]);

        $data= array(
            'list' => $list,
        );

        $this->assign($data);
        $this->setMeta('系统管理|用章申请');
        return view();
    }


    /**
     * 我的流程
     * @return [type] [description]
     */
    public function myprocesslist(){

        $user_id = session('user_auth.user_id');

        $map= array();
        $map['a.type_id'] = 1;//用章
        $map['c.user_id'] = $user_id;

        $list = model('process')->alias('a')
                           ->distinct(true)
                           ->field('a.*')
                           ->join('__PROCESS_APPLY__ b ','b.id= a.id')
                           ->join('__USER_ROLE__ c ','c.role_id= b.role_id')
                           ->where($map)
                           ->paginate(20,false,['query' => request()->param()]);

//echo db()->getLastSql();
        $data= array(
            'list' => $list,
        );


        $this->assign($data);
        $this->setMeta('系统管理|用章申请');
        return view(); 
    }

    /**
     * 选择维护流程
     * @return [type] [description]
     */
    public function whmyprocesslist(){

        $user_id = session('user_auth.user_id');

        $map= array();
        $map['a.type_id'] = 2;//1用章 2维护
        $map['c.user_id'] = $user_id;

        $list = model('process')->alias('a')
                           ->distinct(true)
                           ->field('a.*')
                           ->join('__PROCESS_APPLY__ b ','b.id= a.id')
                           ->join('__USER_ROLE__ c ','c.role_id= b.role_id')
                           ->where($map)
                           ->paginate(20,false,['query' => request()->param()]);

//echo db()->getLastSql();
        $data= array(
            'list' => $list,
        );


        $this->assign($data);
        $this->setMeta('系统管理|维护申请');
        return view(); 
    }


    

    /**
     * 流程信息
     * @return [type] [description]
     */
    public function processinfo(){

        $process_id  = input('process_id/d', 0 );

        $info = model('process')->find($process_id);

        $audits = model('process_audit')->getAudits( $process_id );

        $data= array(
            'info' => $info,
            'audits' => $audits,
            'process_id' => $process_id,
        );
        $this->assign($data);
        $this->setMeta('系统管理|流程信息');
        return view();
    }

    /**
     * 维护流程信息
     * @return [type] [description]
     */
    public function whprocessinfo(){

        $process_id  = input('process_id/d', 0 );

        $info = model('process')->find($process_id);

        $audits = model('process_audit')->getAudits( $process_id );

        $data= array(
            'info' => $info,
            'audits' => $audits,
            'process_id' => $process_id,
        );
        $this->assign($data);
        $this->setMeta('系统管理|流程信息');
        return view();
    }



    /**
     * 
     */
    public function edit(){
        if(IS_AJAX){
            $post   = input('post.');



            $data = array(
                'id'            =>input('post.id/d',0),
                'process_id'    =>input('post.process_id/d',0),
                'status'         =>input('post.status/d',0), // 0 1 
                'max_times'     =>input('post.max_times/d',0),
                'info'          =>input('post.info/s',0),
                'attach'        =>implode( ',' , input('post.attach/a',[])),
                'exp_time_day'      =>input('post.exp_time_day/d',1),
            );
            $process = model('Process')->find($data['process_id']);         
            $data['type_id'] = $process['type_id'];
            $data['equipment_id'] = $process['equipment_id'];
            $data['seal_id'] = $process['seal_id'];

            $data['exp_time'] = date("Y-m-d",strtotime("+".($data['exp_time_day']-1)." day"));
            $data['apply_user_id'] = session('user_auth.user_id');





            $model = model('Useseal');
            if ($data) {

                $result = $model
                    ->isUpdate( !!$data['id'] )
                    ->validate('useseal.add')
                    // ->allowField($allowField )
                    ->save($data);
                if (false === $result) {
                    return $this->error( $model->getError() );
                } else {

                    //更新附件关联表
                    $model->attachs()->detach(); //删除中间联表数据
                    $model->attachs()->saveAll(input('post.attach/a',[]));//增加

                    //如果 状态 提交 创建审核数据
                    if( $data['status'] == 1 ){
                        $model->makeAuditData( $model->process_id );
                    }

                    
                    if($data['status'] == 0 ){
                        return $this->success('保存成功');
                    }else if( $data['status'] == 1  ){
                        return $this->success('保存并提交申请');
                    }
                    
                    return $this->success('提交？？？');
                }
            } else {
                return $this->error( '没有提交数据' );
            }  
        }else{

            
            $id = input('id/d', 0 );
            $process_id  = input('process_id/d', 0 );
            $process = model('process')->find($process_id);
            $type_id = $process['type_id'];





            $info = model('useseal')->find($id);
            $data= array(
                'process_id' => $process_id,
                'process' => $process,
                'type_id' => $type_id,
                'info' => $info,
                );
            //判断是否可以修改 
            if( isset( $info['status'] )  &&  $info['status'] > 0  ){
                $this->error('申请已经提交，无法修改....',url('show').'?id='.$info['id'] );
            };

            //不是申请人 无法修改 
            if( isset( $info['apply_user_id'] )  &&  $info['apply_user_id'] != session('user_auth.user_id')  ){
                $this->error('无法修改他人的申请....',url('show').'?id='.$info['id'] );
            };

            $this->assign($data);
            $this->setMeta('系统管理|用章申请');
            return view();
        }


    }


    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {

        // $id = array_unique((array) input('id/a', [] ));
        // if (empty($id)) {
        //     return $this->error('请选择要删除的数据!');
        // }
        // $map = array('id' => array('in', $id));
        $id = input('id/d', 0 );
        if (!$id) {
            return $this->error('请选择要删除的数据!');
        }

        $rs = model('useseal')->find($id);

        if($rs['status'] > 0 ){
            return $this->error('申请已提交，无法删除...');
        }
        $map = array('id' => array('in', $id ));

        if ( false === model('useseal')->where($map)->setField('status', -2) ) {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }

    /**
     * 显示 审核信息  需要考虑  谁访问  ？？？  申请人访问  ||  审核人方法  || 其他人查看
     * @return [type] [description]
     */
    public function show(){


        $user_id = session('user_auth.user_id');
        $id = input('id/d',0 );
        $info = model('useseal')->find($id);

        //判断是 申请人
        if($user_id == $info['apply_user_id']){
            $user_is_apply = true;
        }

        //判断是 审核人，并且有审核 状态
        $audit_user = model('useseal_audit')->where(['id' => $id , 'user_id' => $user_id , 'status' => 0 ])->find();


        $data= [
            'info' => $info ,
            'audit_user' => $audit_user ,
        ];
        $this->assign($data);
        $this->setMeta('系统管理|用章申请');
        return view();
    }

    /**
     * 审核信息 提交保存
     * @return [type] [description]
     */
    public function auditSave(){
        $data = input('post.');
        if($data['flag'] == 2 && empty( $data['mark']) ){
            $this->error( '审核不通过，必须在批示中填写原因！' );
        }
        $model =  model('useseal_audit');
        $rs =   $model
        ->isUpdate(true)
        ->allowField('flag,mark, id , step ,  user_id ')
        ->save($data);

        if($rs === false){
            
             $this->error(  $model->getError() );
        }else{
            model('useseal')->checkAudit($data['id']);
            $this->success('您的审核提交成功！');
        };

    }



    public function test()
    {

        echo "index";


        model('useseal')->checkAudit(45);
        // $this->assign( $data );
        // $this->setMeta('系统管理|配置管理');
        // return view('index');
    }



    /**
     * 我的 审核列表
     * @return [type] [description]
     */
    public function myauditlist()
    {
        $model = model('useseal');

        $user_id = session('user_auth.user_id');

        $map= array();
        $map['b.user_id'] = $user_id;
        // $map['status'] = ['>',-2];

        $list = model('useseal')->alias('a')
                    ->field( 'a.* , b.status as b_status ,b.step as b_step ')
                    ->join('bc_useseal_audit b', ' b.id = a.id' )
                    ->where($map)
                    ->order( 'a.id desc , step asc ' )
                    // ->select();
                    ->paginate(20,false,['query' => request()->param()]);
// echo db()->getLastSql();
        $data= array(
            'list' => $list,
        );

        $this->assign($data);
        $this->setMeta('系统管理|用章申请');
        return view();



    }




    /**
     * 参考全部 申请
     * @return [type] [description]
     */
    public function allapplylist(){


        $user_id = session('user_auth.user_id');

        $map= array();
        $map['type_id']=1;//  维护请求
        $map['apply_user_id'] = $user_id;
        $map['status'] = ['>',-2];

        $list = model('useseal')
                    ->where($map)
                    ->order( 'id desc' )
                    ->paginate(20,false,['query' => request()->param()]);

        $data= array(
            'list' => $list,
        );

        $this->assign($data);
        $this->setMeta('系统管理|用章申请记录');
        return view();
    }



//----------------------------------------------------------------------
    /**
     * 维护申请 list
     * @return [type] [description]
     */
    public function whallapplylist(){


        $user_id = session('user_auth.user_id');

        $map= array();
        $map['type_id']=2;//  维护请求
        $map['apply_user_id'] = $user_id;
        $map['status'] = ['>',-2];

        $list = model('useseal')
                    ->where($map)
                    ->order( 'id desc' )
                    ->paginate(20,false,['query' => request()->param()]);

        $data= array(
            'list' => $list,
        );

        $this->assign($data);
        $this->setMeta('系统管理|维护申请记录');
        return view();
    }





    

    


}
